//
//  SecurityViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/10.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "SecurityViewController.h"
#import "LZFoldButton.h"

#import "AFNetworking.h"


@interface SecurityViewController ()<LZFoldButtonDelegate,UITextFieldDelegate>
{
    // 下拉button
    LZFoldButton *lz;

//    // 数组
//    NSMutableArray *_securityArray;

    // 问题数组
    NSArray *arr;

    // 取值字符串
    NSString *nss;

    // 答案TextField
    UITextField *securityTextField;

    // 资金密码TextField
    UITextField *securityTextField3;
}

@end

@implementation SecurityViewController


-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"User" forKey:@"Model"];
    [parametersDic setObject:@"SecurityQuestion_Edit" forKey:@"Action"];
    [parametersDic setObject:@"1" forKey:@"type"];
    NSString *NSUTF8 = [NSString stringWithFormat:@"U_SecurityQuestion=%@&U_SecurityAnswer=%@&U_BankPassWord=%@",nss,securityTextField.text,securityTextField3.text];
    NSUTF8 = [NSUTF8 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSLog(@"%@",NSUTF8);
    [parametersDic setValue:NSUTF8 forKey:@"Data"];


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];

        UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:[dic objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alerView addAction:action];
        [self presentViewController:alerView animated:YES completion:nil];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"安全问题";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
//    // 状态栏(statusbar)
//    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
//    
//    // 导航栏（navigationbar）
//    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    UILabel *securityLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0+0+20, UISCREEN_WIDTH, 40)];
    securityLabel.text = @"      问题";
    securityLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:securityLabel];
    
    arr = @[@"您母亲的姓名是？",@"您的父亲姓名是？",@"您的配偶姓名是？",@"您的配偶生日是？",@"您的学号（或工号）是？",@"您的小学班主任的名字是？",@"您的中学班主任的名字是？",@"您的高中班主任的名字是？",@"您的大学班主任的名字是？",@"您最熟悉的童年好友名字是？",@"您最喜欢的明星名字是？",@"您最喜欢的运动是？",@"对您影响最大的人名字是？"];
    
    lz = [[LZFoldButton alloc]initWithFrame:CGRectMake(70, CGRectGetMinY(securityLabel.frame)+5, UISCREEN_WIDTH-70, 30) dataArray:arr];
    
    lz.lzDelegate = self;
    lz.backgroundColor = [UIColor clearColor];
    lz.lzFontSize = 12;
    lz.lzHeight = 130;
    lz.lzButtonType = 1;
    
    [lz LZSetTitle:@"请选择问题                        " forState:UIControlStateNormal];
    [lz LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateNormal];
    [lz LZSetImage:[UIImage imageNamed:@"icon_select_arrow"] forState:UIControlStateSelected];
    [lz LZSetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:lz];
    
    UILabel *securityLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(securityLabel.frame)+1, UISCREEN_WIDTH, 40)];
    securityLabel2.text = @"      答案";
    securityLabel2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:securityLabel2];
    
    securityTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(securityLabel2.frame)+5, UISCREEN_WIDTH-120, 30)];
    securityTextField.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
    securityTextField.clearButtonMode = YES; //清除模式开启
//    securityTextField.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    securityTextField.delegate = self;
    [self.view addSubview:securityTextField];

    UILabel *securityLabel3 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(securityLabel2.frame)+1, UISCREEN_WIDTH, 40)];
    securityLabel3.text = @"      取款密码";
    securityLabel3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:securityLabel3];

    securityTextField3 = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(securityLabel3.frame)+5, UISCREEN_WIDTH-120, 30)];
    securityTextField3.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
    //    _setUpMoneyPasswordTextField3.clearButtonMode = YES; //清除模式开启
    securityTextField3.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    securityTextField3.delegate = self;
    [self.view addSubview:securityTextField3];

    
    UILabel *securityLabel4 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(securityLabel3.frame)+1, UISCREEN_WIDTH, 40)];
    securityLabel4.text = @"        答案包括4-6位字符，可由中文、字母、数字组成";
    securityLabel4.textColor = [UIColor grayColor];
    securityLabel4.backgroundColor = [UIColor clearColor];
    securityLabel4.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:securityLabel4];
    
    UIButton *securityButton = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(securityLabel4.frame)+1, UISCREEN_WIDTH-20-20, 40)];
    [securityButton setTitle:@"下一步" forState:UIControlStateNormal];
    [securityButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    securityButton.backgroundColor = [UIColor colorWithRed:36/255.0f green:11/255.0f blue:54/255.0f alpha:1];
    [securityButton addTarget:self action:@selector(securityAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:securityButton];
}

// 下一步按钮点击事件
- (void)securityAction:(UIButton *)sender {
    [self obtainData];
}

//UITextField控件协议方法：当点击返回按钮时方法执行
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)LZFoldButton:(LZFoldButton*)foldButton didSelectObject:(id)obj {

    nss = obj;

}

@end
